//
//  ApplyBillViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/6.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ApplyBillViewController.h"

@interface ApplyBillViewController ()<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate,UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSDictionary *_areaDic;//地区字典
    NSArray *_province;//省 数组
    NSArray *_city;//市 数组
    NSArray *_district;//区 数组
    
    NSString *_selectedProvince;//选中的省份
    UIButton *_shadowBtn;//蒙尘效果按钮
    
    NSString *_recProv;//省
    NSString *_recCity;//市
    NSString *_recArea;//区
    
}

@property (nonatomic, strong) UIView *pickViewToobar;//pickview的完成工具条
@property (nonatomic, strong) UIPickerView *pickView;
//@property (nonatomic, assign) BOOL isSelect;

//@property (nonatomic, assign) BOOL isPaper;
@property (nonatomic, assign) BOOL isCompany;
@property (nonatomic,assign)  InvoiceType   invoiceType;
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic,strong)  UIButton      *submitButton;

@property (nonatomic, strong) UIButton *eleBtn;
@property (nonatomic, strong) UIButton *paperBtn;
@property (nonatomic, strong) UIButton *personBtn;
@property (nonatomic, strong) UIButton *companyBtn;

@property (nonatomic,strong)  UITextField *titleField;
@property (nonatomic,strong)  UITextField *numberField;
@property (nonatomic,strong)  UITextField *contentField;
@property (nonatomic,strong)  UITextField *connectField;
@property (nonatomic,strong)  UITextField *emailField;
@property (nonatomic,strong)  UITextField *phoneField;
@property (nonatomic,strong)  UITextField *addressField;
@property (nonatomic,strong)  UITextField *detailAddressField;

@end

@implementation ApplyBillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"填写发票信息";
//    _isPaper = NO;
    _isCompany = NO;

    self.invoiceType = InvoiceType_Ele;
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    [self.view addSubview:self.tableView];
    UIView *operateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(155))];
    self.tableView.tableFooterView = operateView;
    
     [self initWithData];
}

-(void)submitInfoClicked
{
    if (self.titleField.text.length <= 0 || self.contentField.text.length <= 0) {
        [HttpManager showNoteMsg:@"请填写完整后再提交"];
//        self.submitButton.enabled = NO;
        return;
    }
    if (self.invoiceType == InvoiceType_Ele) {
        if (self.emailField.text <= 0 ) {
            [HttpManager showNoteMsg:@"请填写邮箱地址"];
            return;
        }
    }else{
        if (self.detailAddressField.text.length <= 0 || self.phoneField.text.length <= 0 || self.connectField.text.length <= 0) {
            [HttpManager showNoteMsg:@"请填写信息完整"];
            return;
        }
    }
    
    [self requestData];
    
}

-(void)selectAddress
{
    [self showPickView];
}

-(void)requestData
{
    NSString *url = [NSString stringWithFormat:@"%@app/invoice_update",REQUESTHEADER];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setValue:[FPLoginAndRegisterTools getToken] forKey:@"token"];
    [dict setValue:@([self.order_id integerValue]) forKey:@"order_id"];//订单id
    [dict setValue:@(self.invoiceType) forKey:@"invoice_type"];//1 电子发票-增值普票  2 纸质发票-增值普票
    [dict setValue:self.titleField.text forKey:@"invoice_title"];//发票抬头
    [dict setValue:self.contentField.text forKey:@"invoice_content"];//发票内容
    if (self.numberField && self.numberField.text.length > 0) {
        [dict setValue:self.numberField.text forKey:@"invoice_shuiwuhao"];//发票税号
    }
    [dict setValue:self.connectField.text forKey:@"invoice_con_name"];//收件人
    [dict setValue:self.emailField.text forKey:@"invoice_con_tel"];//联系方式（邮箱或者电话）
    [dict setValue:self.detailAddressField.text forKey:@"invoice_con_address"];//详细地址
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager showNoteMsg:@"申请成功"];
            [self.tableView reloadData];
            //返回上个界面
            [self.navigationController popViewControllerAnimated:true];
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 5;
    }else if (section == 2) {
        if (self.invoiceType == InvoiceType_Paper) {
            return 4;
        }
        return 1;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *cellId = @"typeCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            self.eleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenX375(6), ScreenX375(113), ScreenX375(34))];
            [self.eleBtn setTitle:@"  电子发票" forState:UIControlStateNormal];
            [self.eleBtn setTitleColor:REXADECIMALCOLOR(@"#333333") forState:UIControlStateNormal];
            self.eleBtn.titleLabel.font = Kfont(16);
            [self.eleBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
            [self.eleBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
            self.eleBtn.selected = YES;
            [self.eleBtn addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:self.eleBtn];
            
            self.paperBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(142), ScreenX375(6), ScreenX375(113), ScreenX375(34))];
            [self.paperBtn setTitle:@"  纸质发票" forState:UIControlStateNormal];
            [self.paperBtn setTitleColor:REXADECIMALCOLOR(@"#333333") forState:UIControlStateNormal];
            self.paperBtn.titleLabel.font = Kfont(16);
            [self.paperBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
            [self.paperBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
            self.paperBtn.selected = NO;
            [self.paperBtn addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:self.paperBtn];
        }
        return cell;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            static NSString *cellId = @"classCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"抬头类型：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.personBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(77), ScreenX375(6), ScreenX375(113), ScreenX375(34))];
                [self.personBtn setTitle:@"  个人" forState:UIControlStateNormal];
                [self.personBtn setTitleColor:REXADECIMALCOLOR(@"#333333") forState:UIControlStateNormal];
                self.personBtn.titleLabel.font = Kfont(16);
                [self.personBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
                [self.personBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
                self.personBtn.selected = YES;
                self.personBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
                self.personBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
                [self.personBtn addTarget:self action:@selector(selectClass:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:self.personBtn];
                
                self.companyBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(199), ScreenX375(6), ScreenX375(113), ScreenX375(34))];
                [self.companyBtn setTitle:@"  企业" forState:UIControlStateNormal];
                [self.companyBtn setTitleColor:REXADECIMALCOLOR(@"#333333") forState:UIControlStateNormal];
                self.companyBtn.titleLabel.font = Kfont(16);
                [self.companyBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
                [self.companyBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
                self.companyBtn.selected = NO;
                self.companyBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
                self.companyBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
                [self.companyBtn addTarget:self action:@selector(selectClass:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:self.companyBtn];
                
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(46) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
                line.image = [UIImage imageNamed:@"虚线"];
                [cell addSubview:line];
            }
            return cell;
        }else if (indexPath.row == 1) {
            static NSString *cellId = @"titleCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"发票抬头：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.titleField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.titleField.delegate = self;
                self.titleField.placeholder = @"填写发票抬头";
                self.titleField.font = Kfont(14);
                [cell addSubview:self.titleField];
                
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(46) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
                line.image = [UIImage imageNamed:@"虚线"];
                [cell addSubview:line];
            }
            return cell;
        }else if (indexPath.row == 2) {
            static NSString *cellId = @"numberCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *subView in cell.subviews) {
                [subView removeFromSuperview];
            }
            if (_isCompany) {
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"税       号：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.numberField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.numberField.delegate = self;
                self.numberField.placeholder = @"填写纳税人识别号";
                self.numberField.font = Kfont(14);
                [cell addSubview:self.numberField];
                
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(46) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
                line.image = [UIImage imageNamed:@"虚线"];
                [cell addSubview:line];
            }
            return cell;
        }else if (indexPath.row == 3) {
            static NSString *cellId = @"contentCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
//                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"发票内容：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.contentField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.contentField.delegate = self;
                self.contentField.placeholder = @"填写发票内容";
                self.contentField.font = Kfont(14);
                [cell addSubview:self.contentField];
                                
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(46) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
                line.image = [UIImage imageNamed:@"虚线"];
                [cell addSubview:line];
            }
            return cell;
        }else {
            static NSString *cellId = @"moneyCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"发票金额：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                moneyLabel.font = Kfont(14);
                moneyLabel.textColor = REXADECIMALCOLOR(@"#c2c2c2");
                [cell addSubview:moneyLabel];
                
                NSString *string = [self.priceNum stringByAppendingString:@"元"];//@"100.00元";
                NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:string];
                [attributedStr addAttribute:NSForegroundColorAttributeName value:ThemeColor range:NSMakeRange(0, self.priceNum.length)];
                moneyLabel.attributedText = attributedStr;
                
            }
            return cell;
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            static NSString *cellId = @"connectCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            for (UIView *subView in cell.subviews) {
                [subView removeFromSuperview];
            }
            if (self.invoiceType == InvoiceType_Paper) {
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"收 件 人：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.connectField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.connectField.delegate = self;
                self.connectField.placeholder = @"填写收件人";
                self.connectField.font = Kfont(14);
                [cell addSubview:self.connectField];
                
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(46) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
                line.image = [UIImage imageNamed:@"虚线"];
                [cell addSubview:line];
            }else {
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"电子邮箱：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.emailField.delegate = self;
                self.emailField.placeholder = @"用于向您发送电子发票";
                self.emailField.font = Kfont(14);
                [cell addSubview:self.emailField];
            }
            return cell;
        }else if (indexPath.row == 1) {
            static NSString *cellId = @"phoneCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"联系电话：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.phoneField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.phoneField.delegate = self;
                self.phoneField.placeholder = @"填写联系电话";
                self.phoneField.font = Kfont(14);
                [cell addSubview:self.phoneField];
                
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(46) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
                line.image = [UIImage imageNamed:@"虚线"];
                [cell addSubview:line];
            }
            return cell;
        }else if (indexPath.row == 2) {
            static NSString *cellId = @"addressCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"所在地区：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.addressField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.addressField.placeholder = @"请选择所在地区";
                self.addressField.font = Kfont(14);
                [cell addSubview:self.addressField];
                
                UIButton *selectAddress = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                [selectAddress addTarget:self action:@selector(selectAddress) forControlEvents:UIControlEventTouchUpInside];
                selectAddress.backgroundColor = [UIColor clearColor];
                [cell addSubview:selectAddress];
                
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(46) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
                line.image = [UIImage imageNamed:@"虚线"];
                [cell addSubview:line];
            }
            return cell;
        }else {
            static NSString *cellId = @"detailAddressCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(80), ScreenX375(46))];
                noteLabel.text = @"详细地址：";
                noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
                noteLabel.font = Kfont(14);
                [cell addSubview:noteLabel];
                
                self.detailAddressField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(86), 0, SCREEN_WIDTH - ScreenX375(110), ScreenX375(46))];
                self.detailAddressField.delegate = self;
                self.detailAddressField.placeholder = @"填写详细地址";
                self.detailAddressField.font = Kfont(14);
                [cell addSubview:self.detailAddressField];
            }
            return cell;
        }
    }else {
        static NSString *cellId = @"operateCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
            
            self.submitButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(50), SCREEN_WIDTH - ScreenX375(26), ScreenX375(44))];
            self.submitButton.backgroundColor = ThemeColor;
            self.submitButton.layer.cornerRadius = 22.0;
            [self.submitButton setTitle:@"提交" forState:UIControlStateNormal];
            [self.submitButton addTarget:self action:@selector(submitInfoClicked) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:self.submitButton];
            
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!_isCompany && indexPath.section == 1 && indexPath.row == 2) {
        return 0;
    }else if (indexPath.section == 3) {
        return ScreenX375(155);
    }
    return ScreenX375(46);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section != 3) {
        return ScreenX375(53);
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return ScreenX375(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section != 3) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(53))];
        view.backgroundColor = [UIColor whiteColor];
        
        UIView *rline = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(15), ScreenX375(3), ScreenX375(23))];
        rline.backgroundColor = ThemeColor;
        [view addSubview:rline];
        
        UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(200), ScreenX375(24))];
        noteLabel.text = section == 0 ? @"请选择发票类型" : (section == 1 ? @"发票详情" : @"接收方式");
        noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
        noteLabel.font = Kfont(18);
        [view addSubview:noteLabel];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(53) - 0.5, SCREEN_WIDTH, 0.5)];
        line.backgroundColor = LineColor;
        [view addSubview:line];
        
        return view;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(10))];
    view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    return view;
}

- (void)selectType:(UIButton *)button {
    if (button == _eleBtn) {
        _eleBtn.selected = YES;
        _paperBtn.selected = NO;
        self.invoiceType = InvoiceType_Ele;
    }else {
        _eleBtn.selected = NO;
        _paperBtn.selected = YES;
        self.invoiceType = InvoiceType_Paper;
    }
    [_tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 1)] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)selectClass:(UIButton *)button {
    if (button == _personBtn) {
        _personBtn.selected = YES;
        _companyBtn.selected = NO;
        _isCompany = NO;
    }else {
        _personBtn.selected = NO;
        _companyBtn.selected = YES;
        _isCompany = YES;
    }
    [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return NO;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:true];
}

//初始化三级联动地址
- (void)initWithData{
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
    _areaDic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    
    NSArray *components = [_areaDic allKeys];
    NSArray *sortedArray = [components sortedArrayUsingComparator: ^(id obj1, id obj2) {
        
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
    
    NSMutableArray *provinceTmp = [[NSMutableArray alloc] init];
    for (int i=0; i<[sortedArray count]; i++) {
        NSString *index = [sortedArray objectAtIndex:i];
        NSArray *tmp = [[_areaDic objectForKey: index] allKeys];
        [provinceTmp addObject: [tmp objectAtIndex:0]];
    }
    
    _province = [[NSArray alloc] initWithArray: provinceTmp];
    
    NSString *index = [sortedArray objectAtIndex:0];
    NSString *selected = [_province objectAtIndex:0];
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [[_areaDic objectForKey:index]objectForKey:selected]];
    
    NSArray *cityArray = [dic allKeys];
    NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary: [dic objectForKey: [cityArray objectAtIndex:0]]];
    _city = [[NSArray alloc] initWithArray: [cityDic allKeys]];
    
    
    NSString *selectedCity = [_city objectAtIndex: 0];
    _district = [[NSArray alloc] initWithArray: [cityDic objectForKey: selectedCity]];
    
    _selectedProvince = [_province objectAtIndex: 0];
    
}

- (void)showPickView {
    [self.view endEditing:YES];
    if (!_pickView){
        _recProv = @"北京市";
        _recCity = @"北京市";
        _recArea = @"东城区";
        
        //工具条
        _pickViewToobar = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT, SCREEN_WIDTH, 44)];
        _pickViewToobar.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_pickViewToobar];
        
        UIButton *cancleBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 40, _pickViewToobar.frame.size.height)];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [cancleBtn addTarget:self action:@selector(pickViewOperateClick:) forControlEvents:UIControlEventTouchUpInside];
        [_pickViewToobar addSubview:cancleBtn];
        
        UIButton *achiveBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 0, 40, _pickViewToobar.frame.size.height)];
        [achiveBtn setTitle:@"完成" forState:UIControlStateNormal];
        achiveBtn.tag = 1;
        [achiveBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [achiveBtn addTarget:self action:@selector(pickViewOperateClick:) forControlEvents:UIControlEventTouchUpInside];
        [_pickViewToobar addSubview:achiveBtn];
        
        _shadowBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TAB_BAR_HEIGHT)];
        [_shadowBtn setBackgroundColor:RGBACOLOR(49, 49, 49, 0.6)];
        [_shadowBtn addTarget:self action:@selector(hiddenPickView) forControlEvents:UIControlEventTouchUpInside];
        [_tableView addSubview:_shadowBtn];
        
        _pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_pickViewToobar.frame), SCREEN_WIDTH, 162)];
        _pickView.delegate = self;
        _pickView.dataSource = self;
        _pickView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_pickView];
    }
    
    //判断pickerView的位置
    if (_pickView.frame.origin.y == SCREEN_HEIGHT+44) {
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self->_pickView.frame = CGRectMake(0, SCREEN_HEIGHT - 162 - NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, 162);
            self->_tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-162 - NAVIGATION_BAR_HEIGHT - TAB_BAR_HEIGHT);
            self->_pickViewToobar.frame = CGRectMake(0,CGRectGetMinY(self->_pickView.frame)-44, SCREEN_WIDTH, 44);
        }];
        _shadowBtn.hidden = NO;
        
    }else{
        
        [UIView animateWithDuration:0.3 animations:^{
            self->_pickView.frame = CGRectMake(0, CGRectGetMaxY(self->_pickViewToobar.frame), SCREEN_WIDTH, 162);
            self->_pickViewToobar.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 44);
        }];
        _shadowBtn.hidden = YES;
    }
}

//隐藏选择器
- (void)hiddenPickView{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self->_pickViewToobar.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 44);
        self->_pickView.frame = CGRectMake(0, CGRectGetMaxY(self->_pickViewToobar.frame), SCREEN_WIDTH, 162);
    }];
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    _shadowBtn.hidden = YES;
}

//点击pickView的工具条操作按钮
- (void)pickViewOperateClick:(UIButton *)sender{
    
    if (sender.tag == 1) {
        //完成
//        addressField
        self.addressField.text = [NSString stringWithFormat:@"%@ %@ %@",_recProv,_recCity,_recArea];
        [self hiddenPickView];
    }else{
        //取消
        [self hiddenPickView];
    }
}

#pragma mark - UIPickView 数据源代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        
        return [_province count];
    }else if (component == 1){
        
        return [_city count];
    }else if (component == 2){
        
        return [_district count];
    }
    return 4;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 0) {
        return [_province objectAtIndex: row];
    }else if (component == 1){
        return [_city objectAtIndex: row];
    }else if (component == 2){
        return [_district objectAtIndex: row];
    }
    return @" ";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    //让三级联动，存在关系跳转
    if (component == 0) {
        _selectedProvince = [_province objectAtIndex: row];
        NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [_areaDic objectForKey: [NSString stringWithFormat:@"%ld", (long)row]]];
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [tmp objectForKey: _selectedProvince]];
        NSArray *cityArray = [dic allKeys];
        NSArray *sortedArray = [cityArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
            
            if ([obj1 integerValue] > [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedDescending;//递减
            }
            
            if ([obj1 integerValue] < [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedAscending;//上升
            }
            return (NSComparisonResult)NSOrderedSame;
        }];
        
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for (int i=0; i<[sortedArray count]; i++) {
            NSString *index = [sortedArray objectAtIndex:i];
            NSArray *temp = [[dic objectForKey: index] allKeys];
            [array addObject: [temp objectAtIndex:0]];
        }
        
        _city = [[NSArray alloc] initWithArray: array];
        
        NSDictionary *cityDic = [dic objectForKey: [sortedArray objectAtIndex: 0]];
        _district = [[NSArray alloc] initWithArray: [cityDic objectForKey: [_city objectAtIndex: 0]]];
        [_pickView selectRow: 0 inComponent: 1 animated: YES];
        [_pickView selectRow: 0 inComponent: 2 animated: YES];
        [_pickView reloadComponent: 1];
        [_pickView reloadComponent: 2];
        
    }
    else if (component == 1) {
        NSString *provinceIndex = [NSString stringWithFormat: @"%lu", (unsigned long)[_province indexOfObject: _selectedProvince]];
        NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [_areaDic objectForKey: provinceIndex]];
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [tmp objectForKey: _selectedProvince]];
        NSArray *dicKeyArray = [dic allKeys];
        NSArray *sortedArray = [dicKeyArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
            
            if ([obj1 integerValue] > [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedDescending;
            }
            
            if ([obj1 integerValue] < [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedAscending;
            }
            return (NSComparisonResult)NSOrderedSame;
        }];
        
        NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary: [dic objectForKey: [sortedArray objectAtIndex: row]]];
        NSArray *cityKeyArray = [cityDic allKeys];
        
        _district = [[NSArray alloc] initWithArray: [cityDic objectForKey: [cityKeyArray objectAtIndex:0]]];
        [_pickView selectRow: 0 inComponent: 2 animated: YES];
        [_pickView reloadComponent: 2];
    }
    
    //获得当前选中的城市
    
    NSInteger provinceIndex = [_pickView selectedRowInComponent:0];
    NSInteger cityIndex = [_pickView selectedRowInComponent:1];
    NSInteger districtIndex = [_pickView selectedRowInComponent:2];
    
    NSString *provinceStr = [_province objectAtIndex: provinceIndex];
    NSString *cityStr = [_city objectAtIndex: cityIndex];
    NSString *districtStr = [_district objectAtIndex:districtIndex];
    
    if ([provinceStr isEqualToString: cityStr] && [cityStr isEqualToString: districtStr]) {
        cityStr = @"";
        districtStr = @"";
    }
    else if ([cityStr isEqualToString: districtStr]) {
        districtStr = @"";
    }
    
    _recProv = provinceStr;
    _recCity = cityStr;
    _recArea = districtStr;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        return 100;
    }
    else if (component == 1) {
        return 100;
    }
    else {
        return 115;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 35.0f;
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (!view){
        view = [[UIView alloc]init];
    }
    CGFloat width = 100.0;
    if (component == 2) {
        width = 115.0;
    }
    
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 35)];
    text.font = Kfont(17);
    if (component == 0) {
        text.text = [_province objectAtIndex:row];
    }
    else if (component == 1) {
        text.textAlignment = NSTextAlignmentCenter;
        text.text = [_city objectAtIndex:row];
    }
    else {
        text.textAlignment = NSTextAlignmentRight;
        text.text = [_district objectAtIndex:row];
    }
    [view addSubview:text];
    
    return view;
}

@end
